﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
//Downloads By http://www.veryhuo.com
namespace 会员卡管理系统
{
    public class Calendar
    {
        /// <summary>
        /// 计算星期几: 星期日至星期六的值为0-6
        /// </summary>
        public static int GetWeeksOfDate(int year, int month, int day)
        {
            DateTime dt = new DateTime(year, month, day);
            DayOfWeek d = dt.DayOfWeek;
            return Convert.ToInt32(d);
        }     
        /// <summary>
        /// 获取指定年月的天数
        /// </summary>
        public static int GetDaysOfMonth(int year, int month)
        {
            DateTime dtCur = new DateTime(year, month, 1);
            int days = dtCur.AddMonths(1).AddDays(-1).Day;
            return days;
        }
        /// <summary>
        /// 获取在图片上生成日历的图片
        /// </summary>
        public static Bitmap GetCalendarPic(Image img)
        {
            Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);
            bmp.SetResolution(72, 72);
            using (Graphics g = Graphics.FromImage(bmp))
            {                
                g.DrawImage(img, 0, 0, img.Width, img.Height);                              
              
                DateTime dtNow = DateTime.Now;
                int year = dtNow.Year;
                int month = dtNow.Month;
                int day = dtNow.Day;
                int day1st = Calendar.GetWeeksOfDate(year, month, 1); //第一天星期几
                int days = Calendar.GetDaysOfMonth(year, month); //获取想要输出月份的天数

                int startX = img.Width / 2; //开始的X轴位置
                int startY = img.Height / 4; //开始的Y轴位置
                int posLen = 50; //每次移动的位置长度
                int x = startX + day1st * posLen; //1号的开始X轴位置
                int y = startY + posLen * 2;//1号的开始Y轴位置

                Calendar.DrawStr(g, dtNow.ToString("yyyy年MM月dd日"), startX, startY);
                string[] weeks = { "日", "一", "二", "三", "四", "五", "六" };
                for (int i = 0; i < weeks.Length; i++)
                    Calendar.DrawStr(g, weeks[i], startX + posLen * i, startY + posLen);

                for (int j = 1; j <= days; j++)
                {
                    if (j == day)//如果是今天，设置背景色
                        Calendar.DrawStrToday(g, j.ToString().PadLeft(2, ' '), x, y);
                    else
                        Calendar.DrawStr(g, j.ToString().PadLeft(2, ' '), x, y);
                    //星期六结束到星期日时换行，X轴回到初始位置，Y轴增加
                    if ((day1st + j) % 7 == 0)
                    {
                        x = startX;
                        y = y + posLen;
                    }
                    else
                        x = x + posLen;
                }
                 
               return bmp;
            }
        }
        /// <summary>
        /// 绘制字符串
        /// </summary>
        public static void DrawStr(Graphics g, string s, float x, float y)
        {
            Font font = new Font("宋体", 25, FontStyle.Bold);
            PointF pointF = new PointF(x, y);
            g.DrawString(s, font, new SolidBrush(Color.Yellow), pointF);
        }
        /// <summary>
        /// 绘制有背景颜色的字符串
        /// </summary>
        public static void DrawStrToday(Graphics g, string s, float x, float y)
        {
            Font font = new Font("宋体", 25, FontStyle.Bold);
            PointF pointF = new PointF(x, y);
            SizeF sizeF = g.MeasureString(s, font);
            g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
            g.DrawString(s, font, Brushes.Black, pointF);
        }
        /// <summary>
        /// 根据图片路径转为Image对象
        /// </summary>
        private Image ReturnImage(string imgPath)
        {
            using (FileStream fs = File.OpenRead(imgPath))
            {
                Image img = Image.FromStream(fs);
                return img;
            }
        }
    }
}
